package com.gutsyzhan.yusiailangchain4j.controller;

import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.document.parser.TextDocumentParser;
import dev.langchain4j.data.document.parser.apache.pdfbox.ApachePdfBoxDocumentParser;
import dev.langchain4j.data.document.splitter.DocumentByCharacterSplitter;
import dev.langchain4j.data.segment.TextSegment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.FileSystems;
import java.nio.file.PathMatcher;
import java.util.List;

@RestController
@RequestMapping("/file")
public class FileController {

    /**
     * 使用FileSystemDocumentLoader读取指定目录下的知识库文档
     * 并使用默认的文档解析器TextDocumentParser对文档进行解析
     */
    @GetMapping("/readOne")
    public String readOne(){
        String filePath = "E:/Files/langchain4j/files/langchain4j.txt";
        Document document = FileSystemDocumentLoader.loadDocument(filePath);
        return document.text();
    }

    /**
     * 从一个目录中加载所有文档
     */
    @GetMapping("/reaAll")
    public String reaAll(){
        String filePath = "E:/Files/langchain4j/files";
        List<Document> documents = FileSystemDocumentLoader.loadDocuments(filePath, new TextDocumentParser());
        return documents.size() + "";
    }

    /**
     * 从一个目录中加载所有的.txt文档
     */
    @GetMapping("/reaAllText")
    public String reaAllText(){
        PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:*.txt");
        String filePath = "E:/Files/langchain4j/files";
        List<Document> documents = FileSystemDocumentLoader.loadDocumentsRecursively(filePath, pathMatcher, new TextDocumentParser());
        return documents.size() + "";
    }

    /**
     * 从一个目录及其子目录中加载所有文档
     */
    @GetMapping("/reaAllFile")
    public String reaAllFile(){
        String filePath = "E:/Files/langchain4j/";
        List<Document> documents = FileSystemDocumentLoader.loadDocumentsRecursively(filePath, new TextDocumentParser());
        return documents.size() + "";
    }

    /**
     *  解析pdf文件
     */
    @GetMapping("/parsePdf")
    public String parsePdf(){
        String filePath = "E:/Files/langchain4j/files/rag.pdf";
        Document document = FileSystemDocumentLoader.loadDocument(filePath, new ApachePdfBoxDocumentParser());
        return document.text();
    }

    /**
     *  测试文本分块
     */
    @GetMapping("/testSplitter")
    public String testSplitter(){
        String filePath = "E:/Files/langchain4j/files/langchain4j.txt";
        Document document = FileSystemDocumentLoader.loadDocument(filePath, new TextDocumentParser());
        // 创建一个文本分块器，将文本分块为多个段落
        DocumentByCharacterSplitter splitter = new DocumentByCharacterSplitter(20,10);
        splitter.split(document);
        // 获取分块后的段落
        List<TextSegment> segments = splitter.split(document);
        return segments.toString();
    }
}
